package ruifeng.zcj.dig.ui.home;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.groble.FragmentFactory;
import ruifeng.zcj.mybase.base.BaseActivity;
import ruifeng.zcj.mybase.base.BaseFragment;

public class MainActivity extends BaseActivity {

    @Bind(R.id.realtabcontent)
    FrameLayout mRealtabcontent;
    @Bind(R.id.btn_tab_1)
    ImageView iv_tab_1;
    @Bind(R.id.btn_tab_2)
    ImageView iv_tab_2;
    @Bind(R.id.btn_tab_3)
    ImageView iv_tab_3;
    @Bind(R.id.btn_tab_4)
    ImageView iv_tab_4;
    @Bind(R.id.btn_tab_5)
    ImageView iv_tab_5;
    @Bind(R.id.tab_btn_menu)
    LinearLayout mTabBtnMenu;

    private FragmentManager manager;
    private FragmentTransaction transaction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  NBSAppAgent.setLicenseKey("e8baec56ecf5425c9c0157d68b023685").start(this.getApplicationContext());
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    public void initView() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        BaseFragment home = FragmentFactory.createFragment(0);
        transaction.replace(R.id.realtabcontent, home);
        transaction.commitAllowingStateLoss();
//        transaction.commit();
        iv_tab_1.setSelected(true);

        allListener();
    }

    public void switchFragment(int position) {
        transaction = manager.beginTransaction();
        BaseFragment home = null;
        switch (position) {
            case 0:// 首页的切换
                iv_tab_1.setSelected(true);
                iv_tab_2.setSelected(false);
                iv_tab_3.setSelected(false);
                iv_tab_4.setSelected(false);
                iv_tab_5.setSelected(false);
                home = FragmentFactory.createFragment(0);
                break;
            case 1:
                iv_tab_1.setSelected(false);
                iv_tab_2.setSelected(true);
                iv_tab_3.setSelected(false);
                iv_tab_4.setSelected(false);
                iv_tab_5.setSelected(false);
                transaction = manager.beginTransaction();
                home = FragmentFactory.createFragment(1);
                break;
            case 2:
                iv_tab_1.setSelected(false);
                iv_tab_2.setSelected(false);
                iv_tab_3.setSelected(true);
                iv_tab_4.setSelected(false);
                iv_tab_5.setSelected(false);
                home = FragmentFactory.createFragment(2);
                break;
            case 3:
                iv_tab_1.setSelected(false);
                iv_tab_2.setSelected(false);
                iv_tab_3.setSelected(false);
                iv_tab_4.setSelected(true);
                iv_tab_5.setSelected(false);
                home = FragmentFactory.createFragment(3);
                break;
            case 4:
                iv_tab_1.setSelected(false);
                iv_tab_2.setSelected(false);
                iv_tab_3.setSelected(false);
                iv_tab_4.setSelected(false);
                iv_tab_5.setSelected(true);
                home = FragmentFactory.createFragment(4);
                break;
            case 5:
                break;
            default:
               /* iv_tab_1.setSelected(true);
                iv_tab_2.setSelected(false);
                iv_tab_3.setSelected(false);
                iv_tab_4.setSelected(false);
                iv_tab_5.setSelected(false);
                home_press = FragmentFactory.createFragment(position);*/
                break;
        }
        transaction.replace(R.id.realtabcontent, home);
//        transaction.commit();
        transaction.commitAllowingStateLoss();
    }

    private void allListener() {
        iv_tab_1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switchFragment(0);
            }
        });

        iv_tab_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switchFragment(1);
            }
        });

        iv_tab_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switchFragment(2);
            }
        });

        iv_tab_4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switchFragment(3);
            }
        });
        iv_tab_5.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switchFragment(4);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click();
        }
        return false;
    }

    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true;
            showSnackBar("再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();


    }


}
